1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package java.util;
27 import java.io.Serializable;
28 import java.io.ObjectOutputStream;
29 import java.io.IOException;
30 import java.lang.reflect.Array;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public class Collections {
72
73 private Collections() {
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 private static final int BINARYSEARCH_THRESHOLD = 5000;
93 private static final int REVERSE_THRESHOLD = 18;
94 private static final int SHUFFLE_THRESHOLD = 5;
95 private static final int FILL_THRESHOLD = 25;
96 private static final int ROTATE_THRESHOLD = 100;
97 private static final int COPY_THRESHOLD = 10;
98 private static final int REPLACEALL_THRESHOLD = 11;
99 private static final int INDEXOFSUBLIST_THRESHOLD = 35;
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public static <T extends Comparable<? super T>> void sort(List<T> list) {
154 Object[] a = list.toArray();
155 Arrays.sort(a);
156 ListIterator<T> i = list.listIterator();
157 for (int j=0; j<a.length; j++) {
158 i.next();
159 i.set((T)a[j]);
160 }
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 public static <T> void sort(List<T> list, Comparator<? super T> c) {
216 Object[] a = list.toArray();
217 Arrays.sort(a, (Comparator)c);
218 ListIterator i = list.listIterator();
219 for (int j=0; j<a.length; j++) {
220 i.next();
221 i.set(a[j]);
222 }
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256 public static <T>
257 int binarySearch(List<? extends Comparable<? super T>> list, T key) {
258 if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
259 return Collections.indexedBinarySearch(list, key);
260 else
261 return Collections.iteratorBinarySearch(list, key);
262 }
263
264 private static <T>
265 int indexedBinarySearch(List<? extends Comparable<? super T>> list, T key)
266 {
267 int low = 0;
268 int high = list.size()-1;
269
270 while (low <= high) {
271 int mid = (low + high) >>> 1;
272 Comparable<? super T> midVal = list.get(mid);
273 int cmp = midVal.compareTo(key);
274
275 if (cmp < 0)
276 low = mid + 1;
277 else if (cmp > 0)
278 high = mid - 1;
279 else
280 return mid;
281 }
282 return -(low + 1);
283 }
284
285 private static <T>
286 int iteratorBinarySearch(List<? extends Comparable<? super T>> list, T key)
287 {
288 int low = 0;
289 int high = list.size()-1;
290 ListIterator<? extends Comparable<? super T>> i = list.listIterator();
291
292 while (low <= high) {
293 int mid = (low + high) >>> 1;
294 Comparable<? super T> midVal = get(i, mid);
295 int cmp = midVal.compareTo(key);
296
297 if (cmp < 0)
298 low = mid + 1;
299 else if (cmp > 0)
300 high = mid - 1;
301 else
302 return mid;
303 }
304 return -(low + 1);
305 }
306
307
308
309
310
311 private static <T> T get(ListIterator<? extends T> i, int index) {
312 T obj = null;
313 int pos = i.nextIndex();
314 if (pos <= index) {
315 do {
316 obj = i.next();
317 } while (pos++ < index);
318 } else {
319 do {
320 obj = i.previous();
321 } while (--pos > index);
322 }
323 return obj;
324 }
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360 public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) {
361 if (c==null)
362 return binarySearch((List) list, key);
363
364 if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
365 return Collections.indexedBinarySearch(list, key, c);
366 else
367 return Collections.iteratorBinarySearch(list, key, c);
368 }
369
370 private static <T> int indexedBinarySearch(List<? extends T> l, T key, Comparator<? super T> c) {
371 int low = 0;
372 int high = l.size()-1;
373
374 while (low <= high) {
375 int mid = (low + high) >>> 1;
376 T midVal = l.get(mid);
377 int cmp = c.compare(midVal, key);
378
379 if (cmp < 0)
380 low = mid + 1;
381 else if (cmp > 0)
382 high = mid - 1;
383 else
384 return mid;
385 }
386 return -(low + 1);
387 }
388
389 private static <T> int iteratorBinarySearch(List<? extends T> l, T key, Comparator<? super T> c) {
390 int low = 0;
391 int high = l.size()-1;
392 ListIterator<? extends T> i = l.listIterator();
393
394 while (low <= high) {
395 int mid = (low + high) >>> 1;
396 T midVal = get(i, mid);
397 int cmp = c.compare(midVal, key);
398
399 if (cmp < 0)
400 low = mid + 1;
401 else if (cmp > 0)
402 high = mid - 1;
403 else
404 return mid;
405 }
406 return -(low + 1);
407 }
408
409 private interface SelfComparable extends Comparable<SelfComparable> {}
410
411
412
413
414
415
416
417
418
419
420
421 public static void reverse(List<?> list) {
422 int size = list.size();
423 if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) {
424 for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--)
425 swap(list, i, j);
426 } else {
427 ListIterator fwd = list.listIterator();
428 ListIterator rev = list.listIterator(size);
429 for (int i=0, mid=list.size()>>1; i<mid; i++) {
430 Object tmp = fwd.next();
431 fwd.set(rev.previous());
432 rev.set(tmp);
433 }
434 }
435 }
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465 public static void shuffle(List<?> list) {
466 Random rnd = r;
467 if (rnd == null)
468 r = rnd = new Random();
469 shuffle(list, rnd);
470 }
471 private static Random r;
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496 public static void shuffle(List<?> list, Random rnd) {
497 int size = list.size();
498 if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
499 for (int i=size; i>1; i--)
500 swap(list, i-1, rnd.nextInt(i));
501 } else {
502 Object arr[] = list.toArray();
503
504
505 for (int i=size; i>1; i--)
506 swap(arr, i-1, rnd.nextInt(i));
507
508
509 ListIterator it = list.listIterator();
510 for (int i=0; i<arr.length; i++) {
511 it.next();
512 it.set(arr[i]);
513 }
514 }
515 }
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530 public static void swap(List<?> list, int i, int j) {
531 final List l = list;
532 l.set(i, l.set(j, l.get(i)));
533 }
534
535
536
537
538 private static void swap(Object[] arr, int i, int j) {
539 Object tmp = arr[i];
540 arr[i] = arr[j];
541 arr[j] = tmp;
542 }
543
544
545
546
547
548
549
550
551
552
553
554
555 public static <T> void fill(List<? super T> list, T obj) {
556 int size = list.size();
557
558 if (size < FILL_THRESHOLD || list instanceof RandomAccess) {
559 for (int i=0; i<size; i++)
560 list.set(i, obj);
561 } else {
562 ListIterator<? super T> itr = list.listIterator();
563 for (int i=0; i<size; i++) {
564 itr.next();
565 itr.set(obj);
566 }
567 }
568 }
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586 public static <T> void copy(List<? super T> dest, List<? extends T> src) {
587 int srcSize = src.size();
588 if (srcSize > dest.size())
589 throw new IndexOutOfBoundsException("Source does not fit in dest");
590
591 if (srcSize < COPY_THRESHOLD ||
592 (src instanceof RandomAccess && dest instanceof RandomAccess)) {
593 for (int i=0; i<srcSize; i++)
594 dest.set(i, src.get(i));
595 } else {
596 ListIterator<? super T> di=dest.listIterator();
597 ListIterator<? extends T> si=src.listIterator();
598 for (int i=0; i<srcSize; i++) {
599 di.next();
600 di.set(si.next());
601 }
602 }
603 }
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626 public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) {
627 Iterator<? extends T> i = coll.iterator();
628 T candidate = i.next();
629
630 while (i.hasNext()) {
631 T next = i.next();
632 if (next.compareTo(candidate) < 0)
633 candidate = next;
634 }
635 return candidate;
636 }
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660 public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) {
661 if (comp==null)
662 return (T)min((Collection<SelfComparable>) (Collection) coll);
663
664 Iterator<? extends T> i = coll.iterator();
665 T candidate = i.next();
666
667 while (i.hasNext()) {
668 T next = i.next();
669 if (comp.compare(next, candidate) < 0)
670 candidate = next;
671 }
672 return candidate;
673 }
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696 public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {
697 Iterator<? extends T> i = coll.iterator();
698 T candidate = i.next();
699
700 while (i.hasNext()) {
701 T next = i.next();
702 if (next.compareTo(candidate) > 0)
703 candidate = next;
704 }
705 return candidate;
706 }
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730 public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) {
731 if (comp==null)
732 return (T)max((Collection<SelfComparable>) (Collection) coll);
733
734 Iterator<? extends T> i = coll.iterator();
735 T candidate = i.next();
736
737 while (i.hasNext()) {
738 T next = i.next();
739 if (comp.compare(next, candidate) > 0)
740 candidate = next;
741 }
742 return candidate;
743 }
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800 public static void rotate(List<?> list, int distance) {
801 if (list instanceof RandomAccess || list.size() < ROTATE_THRESHOLD)
802 rotate1(list, distance);
803 else
804 rotate2(list, distance);
805 }
806
807 private static <T> void rotate1(List<T> list, int distance) {
808 int size = list.size();
809 if (size == 0)
810 return;
811 distance = distance % size;
812 if (distance < 0)
813 distance += size;
814 if (distance == 0)
815 return;
816
817 for (int cycleStart = 0, nMoved = 0; nMoved != size; cycleStart++) {
818 T displaced = list.get(cycleStart);
819 int i = cycleStart;
820 do {
821 i += distance;
822 if (i >= size)
823 i -= size;
824 displaced = list.set(i, displaced);
825 nMoved ++;
826 } while (i != cycleStart);
827 }
828 }
829
830 private static void rotate2(List<?> list, int distance) {
831 int size = list.size();
832 if (size == 0)
833 return;
834 int mid = -distance % size;
835 if (mid < 0)
836 mid += size;
837 if (mid == 0)
838 return;
839
840 reverse(list.subList(0, mid));
841 reverse(list.subList(mid, size));
842 reverse(list);
843 }
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863 public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) {
864 boolean result = false;
865 int size = list.size();
866 if (size < REPLACEALL_THRESHOLD || list instanceof RandomAccess) {
867 if (oldVal==null) {
868 for (int i=0; i<size; i++) {
869 if (list.get(i)==null) {
870 list.set(i, newVal);
871 result = true;
872 }
873 }
874 } else {
875 for (int i=0; i<size; i++) {
876 if (oldVal.equals(list.get(i))) {
877 list.set(i, newVal);
878 result = true;
879 }
880 }
881 }
882 } else {
883 ListIterator<T> itr=list.listIterator();
884 if (oldVal==null) {
885 for (int i=0; i<size; i++) {
886 if (itr.next()==null) {
887 itr.set(newVal);
888 result = true;
889 }
890 }
891 } else {
892 for (int i=0; i<size; i++) {
893 if (oldVal.equals(itr.next())) {
894 itr.set(newVal);
895 result = true;
896 }
897 }
898 }
899 }
900 return result;
901 }
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923 public static int indexOfSubList(List<?> source, List<?> target) {
924 int sourceSize = source.size();
925 int targetSize = target.size();
926 int maxCandidate = sourceSize - targetSize;
927
928 if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
929 (source instanceof RandomAccess&&target instanceof RandomAccess)) {
930 nextCand:
931 for (int candidate = 0; candidate <= maxCandidate; candidate++) {
932 for (int i=0, j=candidate; i<targetSize; i++, j++)
933 if (!eq(target.get(i), source.get(j)))
934 continue nextCand;
935 return candidate;
936 }
937 } else {
938 ListIterator<?> si = source.listIterator();
939 nextCand:
940 for (int candidate = 0; candidate <= maxCandidate; candidate++) {
941 ListIterator<?> ti = target.listIterator();
942 for (int i=0; i<targetSize; i++) {
943 if (!eq(ti.next(), si.next())) {
944
945 for (int j=0; j<i; j++)
946 si.previous();
947 continue nextCand;
948 }
949 }
950 return candidate;
951 }
952 }
953 return -1;
954 }
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976 public static int lastIndexOfSubList(List<?> source, List<?> target) {
977 int sourceSize = source.size();
978 int targetSize = target.size();
979 int maxCandidate = sourceSize - targetSize;
980
981 if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
982 source instanceof RandomAccess) {
983 nextCand:
984 for (int candidate = maxCandidate; candidate >= 0; candidate--) {
985 for (int i=0, j=candidate; i<targetSize; i++, j++)
986 if (!eq(target.get(i), source.get(j)))
987 continue nextCand;
988 return candidate;
989 }
990 } else {
991 if (maxCandidate < 0)
992 return -1;
993 ListIterator<?> si = source.listIterator(maxCandidate);
994 nextCand:
995 for (int candidate = maxCandidate; candidate >= 0; candidate--) {
996 ListIterator<?> ti = target.listIterator();
997 for (int i=0; i<targetSize; i++) {
998 if (!eq(ti.next(), si.next())) {
999 if (candidate != 0) {
1000
1001 for (int j=0; j<=i+1; j++)
1002 si.previous();
1003 }
1004 continue nextCand;
1005 }
1006 }
1007 return candidate;
1008 }
1009 }
1010 return -1;
1011 }
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
1038 return new UnmodifiableCollection<>(c);
1039 }
1040
1041
1042
1043
1044 static class UnmodifiableCollection<E> implements Collection<E>, Serializable {
1045 private static final long serialVersionUID = 1820017752578914078L;
1046
1047 final Collection<? extends E> c;
1048
1049 UnmodifiableCollection(Collection<? extends E> c) {
1050 if (c==null)
1051 throw new NullPointerException();
1052 this.c = c;
1053 }
1054
1055 public int size() {return c.size();}
1056 public boolean isEmpty() {return c.isEmpty();}
1057 public boolean contains(Object o) {return c.contains(o);}
1058 public Object[] toArray() {return c.toArray();}
1059 public <T> T[] toArray(T[] a) {return c.toArray(a);}
1060 public String toString() {return c.toString();}
1061
1062 public Iterator<E> iterator() {
1063 return new Iterator<E>() {
1064 private final Iterator<? extends E> i = c.iterator();
1065
1066 public boolean hasNext() {return i.hasNext();}
1067 public E next() {return i.next();}
1068 public void remove() {
1069 throw new UnsupportedOperationException();
1070 }
1071 };
1072 }
1073
1074 public boolean add(E e) {
1075 throw new UnsupportedOperationException();
1076 }
1077 public boolean remove(Object o) {
1078 throw new UnsupportedOperationException();
1079 }
1080
1081 public boolean containsAll(Collection<?> coll) {
1082 return c.containsAll(coll);
1083 }
1084 public boolean addAll(Collection<? extends E> coll) {
1085 throw new UnsupportedOperationException();
1086 }
1087 public boolean removeAll(Collection<?> coll) {
1088 throw new UnsupportedOperationException();
1089 }
1090 public boolean retainAll(Collection<?> coll) {
1091 throw new UnsupportedOperationException();
1092 }
1093 public void clear() {
1094 throw new UnsupportedOperationException();
1095 }
1096 }
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111 public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
1112 return new UnmodifiableSet<>(s);
1113 }
1114
1115
1116
1117
1118 static class UnmodifiableSet<E> extends UnmodifiableCollection<E>
1119 implements Set<E>, Serializable {
1120 private static final long serialVersionUID = -9215047833775013803L;
1121
1122 UnmodifiableSet(Set<? extends E> s) {super(s);}
1123 public boolean equals(Object o) {return o == this || c.equals(o);}
1124 public int hashCode() {return c.hashCode();}
1125 }
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143 public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) {
1144 return new UnmodifiableSortedSet<>(s);
1145 }
1146
1147
1148
1149
1150 static class UnmodifiableSortedSet<E>
1151 extends UnmodifiableSet<E>
1152 implements SortedSet<E>, Serializable {
1153 private static final long serialVersionUID = -4929149591599911165L;
1154 private final SortedSet<E> ss;
1155
1156 UnmodifiableSortedSet(SortedSet<E> s) {super(s); ss = s;}
1157
1158 public Comparator<? super E> comparator() {return ss.comparator();}
1159
1160 public SortedSet<E> subSet(E fromElement, E toElement) {
1161 return new UnmodifiableSortedSet<>(ss.subSet(fromElement,toElement));
1162 }
1163 public SortedSet<E> headSet(E toElement) {
1164 return new UnmodifiableSortedSet<>(ss.headSet(toElement));
1165 }
1166 public SortedSet<E> tailSet(E fromElement) {
1167 return new UnmodifiableSortedSet<>(ss.tailSet(fromElement));
1168 }
1169
1170 public E first() {return ss.first();}
1171 public E last() {return ss.last();}
1172 }
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189 public static <T> List<T> unmodifiableList(List<? extends T> list) {
1190 return (list instanceof RandomAccess ?
1191 new UnmodifiableRandomAccessList<>(list) :
1192 new UnmodifiableList<>(list));
1193 }
1194
1195
1196
1197
1198 static class UnmodifiableList<E> extends UnmodifiableCollection<E>
1199 implements List<E> {
1200 private static final long serialVersionUID = -283967356065247728L;
1201 final List<? extends E> list;
1202
1203 UnmodifiableList(List<? extends E> list) {
1204 super(list);
1205 this.list = list;
1206 }
1207
1208 public boolean equals(Object o) {return o == this || list.equals(o);}
1209 public int hashCode() {return list.hashCode();}
1210
1211 public E get(int index) {return list.get(index);}
1212 public E set(int index, E element) {
1213 throw new UnsupportedOperationException();
1214 }
1215 public void add(int index, E element) {
1216 throw new UnsupportedOperationException();
1217 }
1218 public E remove(int index) {
1219 throw new UnsupportedOperationException();
1220 }
1221 public int indexOf(Object o) {return list.indexOf(o);}
1222 public int lastIndexOf(Object o) {return list.lastIndexOf(o);}
1223 public boolean addAll(int index, Collection<? extends E> c) {
1224 throw new UnsupportedOperationException();
1225 }
1226 public ListIterator<E> listIterator() {return listIterator(0);}
1227
1228 public ListIterator<E> listIterator(final int index) {
1229 return new ListIterator<E>() {
1230 private final ListIterator<? extends E> i
1231 = list.listIterator(index);
1232
1233 public boolean hasNext() {return i.hasNext();}
1234 public E next() {return i.next();}
1235 public boolean hasPrevious() {return i.hasPrevious();}
1236 public E previous() {return i.previous();}
1237 public int nextIndex() {return i.nextIndex();}
1238 public int previousIndex() {return i.previousIndex();}
1239
1240 public void remove() {
1241 throw new UnsupportedOperationException();
1242 }
1243 public void set(E e) {
1244 throw new UnsupportedOperationException();
1245 }
1246 public void add(E e) {
1247 throw new UnsupportedOperationException();
1248 }
1249 };
1250 }
1251
1252 public List<E> subList(int fromIndex, int toIndex) {
1253 return new UnmodifiableList<>(list.subList(fromIndex, toIndex));
1254 }
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268 private Object readResolve() {
1269 return (list instanceof RandomAccess
1270 ? new UnmodifiableRandomAccessList<>(list)
1271 : this);
1272 }
1273 }
1274
1275
1276
1277
1278 static class UnmodifiableRandomAccessList<E> extends UnmodifiableList<E>
1279 implements RandomAccess
1280 {
1281 UnmodifiableRandomAccessList(List<? extends E> list) {
1282 super(list);
1283 }
1284
1285 public List<E> subList(int fromIndex, int toIndex) {
1286 return new UnmodifiableRandomAccessList<>(
1287 list.subList(fromIndex, toIndex));
1288 }
1289
1290 private static final long serialVersionUID = -2542308836966382001L;
1291
1292
1293
1294
1295
1296
1297
1298 private Object writeReplace() {
1299 return new UnmodifiableList<>(list);
1300 }
1301 }
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317 public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) {
1318 return new UnmodifiableMap<>(m);
1319 }
1320
1321
1322
1323
1324 private static class UnmodifiableMap<K,V> implements Map<K,V>, Serializable {
1325 private static final long serialVersionUID = -1034234728574286014L;
1326
1327 private final Map<? extends K, ? extends V> m;
1328
1329 UnmodifiableMap(Map<? extends K, ? extends V> m) {
1330 if (m==null)
1331 throw new NullPointerException();
1332 this.m = m;
1333 }
1334
1335 public int size() {return m.size();}
1336 public boolean isEmpty() {return m.isEmpty();}
1337 public boolean containsKey(Object key) {return m.containsKey(key);}
1338 public boolean containsValue(Object val) {return m.containsValue(val);}
1339 public V get(Object key) {return m.get(key);}
1340
1341 public V put(K key, V value) {
1342 throw new UnsupportedOperationException();
1343 }
1344 public V remove(Object key) {
1345 throw new UnsupportedOperationException();
1346 }
1347 public void putAll(Map<? extends K, ? extends V> m) {
1348 throw new UnsupportedOperationException();
1349 }
1350 public void clear() {
1351 throw new UnsupportedOperationException();
1352 }
1353
1354 private transient Set<K> keySet = null;
1355 private transient Set<Map.Entry<K,V>> entrySet = null;
1356 private transient Collection<V> values = null;
1357
1358 public Set<K> keySet() {
1359 if (keySet==null)
1360 keySet = unmodifiableSet(m.keySet());
1361 return keySet;
1362 }
1363
1364 public Set<Map.Entry<K,V>> entrySet() {
1365 if (entrySet==null)
1366 entrySet = new UnmodifiableEntrySet<>(m.entrySet());
1367 return entrySet;
1368 }
1369
1370 public Collection<V> values() {
1371 if (values==null)
1372 values = unmodifiableCollection(m.values());
1373 return values;
1374 }
1375
1376 public boolean equals(Object o) {return o == this || m.equals(o);}
1377 public int hashCode() {return m.hashCode();}
1378 public String toString() {return m.toString();}
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388 static class UnmodifiableEntrySet<K,V>
1389 extends UnmodifiableSet<Map.Entry<K,V>> {
1390 private static final long serialVersionUID = 7854390611657943733L;
1391
1392 UnmodifiableEntrySet(Set<? extends Map.Entry<? extends K, ? extends V>> s) {
1393 super((Set)s);
1394 }
1395 public Iterator<Map.Entry<K,V>> iterator() {
1396 return new Iterator<Map.Entry<K,V>>() {
1397 private final Iterator<? extends Map.Entry<? extends K, ? extends V>> i = c.iterator();
1398
1399 public boolean hasNext() {
1400 return i.hasNext();
1401 }
1402 public Map.Entry<K,V> next() {
1403 return new UnmodifiableEntry<>(i.next());
1404 }
1405 public void remove() {
1406 throw new UnsupportedOperationException();
1407 }
1408 };
1409 }
1410
1411 public Object[] toArray() {
1412 Object[] a = c.toArray();
1413 for (int i=0; i<a.length; i++)
1414 a[i] = new UnmodifiableEntry<>((Map.Entry<K,V>)a[i]);
1415 return a;
1416 }
1417
1418 public <T> T[] toArray(T[] a) {
1419
1420
1421
1422 Object[] arr = c.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
1423
1424 for (int i=0; i<arr.length; i++)
1425 arr[i] = new UnmodifiableEntry<>((Map.Entry<K,V>)arr[i]);
1426
1427 if (arr.length > a.length)
1428 return (T[])arr;
1429
1430 System.arraycopy(arr, 0, a, 0, arr.length);
1431 if (a.length > arr.length)
1432 a[arr.length] = null;
1433 return a;
1434 }
1435
1436
1437
1438
1439
1440
1441
1442 public boolean contains(Object o) {
1443 if (!(o instanceof Map.Entry))
1444 return false;
1445 return c.contains(
1446 new UnmodifiableEntry<>((Map.Entry<?,?>) o));
1447 }
1448
1449
1450
1451
1452
1453
1454 public boolean containsAll(Collection<?> coll) {
1455 for (Object e : coll) {
1456 if (!contains(e))
1457 return false;
1458 }
1459 return true;
1460 }
1461 public boolean equals(Object o) {
1462 if (o == this)
1463 return true;
1464
1465 if (!(o instanceof Set))
1466 return false;
1467 Set s = (Set) o;
1468 if (s.size() != c.size())
1469 return false;
1470 return containsAll(s);
1471 }
1472
1473
1474
1475
1476
1477
1478
1479
1480 private static class UnmodifiableEntry<K,V> implements Map.Entry<K,V> {
1481 private Map.Entry<? extends K, ? extends V> e;
1482
1483 UnmodifiableEntry(Map.Entry<? extends K, ? extends V> e) {this.e = e;}
1484
1485 public K getKey() {return e.getKey();}
1486 public V getValue() {return e.getValue();}
1487 public V setValue(V value) {
1488 throw new UnsupportedOperationException();
1489 }
1490 public int hashCode() {return e.hashCode();}
1491 public boolean equals(Object o) {
1492 if (!(o instanceof Map.Entry))
1493 return false;
1494 Map.Entry t = (Map.Entry)o;
1495 return eq(e.getKey(), t.getKey()) &&
1496 eq(e.getValue(), t.getValue());
1497 }
1498 public String toString() {return e.toString();}
1499 }
1500 }
1501 }
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519 public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) {
1520 return new UnmodifiableSortedMap<>(m);
1521 }
1522
1523
1524
1525
1526 static class UnmodifiableSortedMap<K,V>
1527 extends UnmodifiableMap<K,V>
1528 implements SortedMap<K,V>, Serializable {
1529 private static final long serialVersionUID = -8806743815996713206L;
1530
1531 private final SortedMap<K, ? extends V> sm;
1532
1533 UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m;}
1534
1535 public Comparator<? super K> comparator() {return sm.comparator();}
1536
1537 public SortedMap<K,V> subMap(K fromKey, K toKey) {
1538 return new UnmodifiableSortedMap<>(sm.subMap(fromKey, toKey));
1539 }
1540 public SortedMap<K,V> headMap(K toKey) {
1541 return new UnmodifiableSortedMap<>(sm.headMap(toKey));
1542 }
1543 public SortedMap<K,V> tailMap(K fromKey) {
1544 return new UnmodifiableSortedMap<>(sm.tailMap(fromKey));
1545 }
1546
1547 public K firstKey() {return sm.firstKey();}
1548 public K lastKey() {return sm.lastKey();}
1549 }
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585 public static <T> Collection<T> synchronizedCollection(Collection<T> c) {
1586 return new SynchronizedCollection<>(c);
1587 }
1588
1589 static <T> Collection<T> synchronizedCollection(Collection<T> c, Object mutex) {
1590 return new SynchronizedCollection<>(c, mutex);
1591 }
1592
1593
1594
1595
1596 static class SynchronizedCollection<E> implements Collection<E>, Serializable {
1597 private static final long serialVersionUID = 3053995032091335093L;
1598
1599 final Collection<E> c;
1600 final Object mutex;
1601
1602 SynchronizedCollection(Collection<E> c) {
1603 if (c==null)
1604 throw new NullPointerException();
1605 this.c = c;
1606 mutex = this;
1607 }
1608 SynchronizedCollection(Collection<E> c, Object mutex) {
1609 this.c = c;
1610 this.mutex = mutex;
1611 }
1612
1613 public int size() {
1614 synchronized (mutex) {return c.size();}
1615 }
1616 public boolean isEmpty() {
1617 synchronized (mutex) {return c.isEmpty();}
1618 }
1619 public boolean contains(Object o) {
1620 synchronized (mutex) {return c.contains(o);}
1621 }
1622 public Object[] toArray() {
1623 synchronized (mutex) {return c.toArray();}
1624 }
1625 public <T> T[] toArray(T[] a) {
1626 synchronized (mutex) {return c.toArray(a);}
1627 }
1628
1629 public Iterator<E> iterator() {
1630 return c.iterator();
1631 }
1632
1633 public boolean add(E e) {
1634 synchronized (mutex) {return c.add(e);}
1635 }
1636 public boolean remove(Object o) {
1637 synchronized (mutex) {return c.remove(o);}
1638 }
1639
1640 public boolean containsAll(Collection<?> coll) {
1641 synchronized (mutex) {return c.containsAll(coll);}
1642 }
1643 public boolean addAll(Collection<? extends E> coll) {
1644 synchronized (mutex) {return c.addAll(coll);}
1645 }
1646 public boolean removeAll(Collection<?> coll) {
1647 synchronized (mutex) {return c.removeAll(coll);}
1648 }
1649 public boolean retainAll(Collection<?> coll) {
1650 synchronized (mutex) {return c.retainAll(coll);}
1651 }
1652 public void clear() {
1653 synchronized (mutex) {c.clear();}
1654 }
1655 public String toString() {
1656 synchronized (mutex) {return c.toString();}
1657 }
1658 private void writeObject(ObjectOutputStream s) throws IOException {
1659 synchronized (mutex) {s.defaultWriteObject();}
1660 }
1661 }
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688 public static <T> Set<T> synchronizedSet(Set<T> s) {
1689 return new SynchronizedSet<>(s);
1690 }
1691
1692 static <T> Set<T> synchronizedSet(Set<T> s, Object mutex) {
1693 return new SynchronizedSet<>(s, mutex);
1694 }
1695
1696
1697
1698
1699 static class SynchronizedSet<E>
1700 extends SynchronizedCollection<E>
1701 implements Set<E> {
1702 private static final long serialVersionUID = 487447009682186044L;
1703
1704 SynchronizedSet(Set<E> s) {
1705 super(s);
1706 }
1707 SynchronizedSet(Set<E> s, Object mutex) {
1708 super(s, mutex);
1709 }
1710
1711 public boolean equals(Object o) {
1712 synchronized (mutex) {return c.equals(o);}
1713 }
1714 public int hashCode() {
1715 synchronized (mutex) {return c.hashCode();}
1716 }
1717 }
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756 public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) {
1757 return new SynchronizedSortedSet<>(s);
1758 }
1759
1760
1761
1762
1763 static class SynchronizedSortedSet<E>
1764 extends SynchronizedSet<E>
1765 implements SortedSet<E>
1766 {
1767 private static final long serialVersionUID = 8695801310862127406L;
1768
1769 private final SortedSet<E> ss;
1770
1771 SynchronizedSortedSet(SortedSet<E> s) {
1772 super(s);
1773 ss = s;
1774 }
1775 SynchronizedSortedSet(SortedSet<E> s, Object mutex) {
1776 super(s, mutex);
1777 ss = s;
1778 }
1779
1780 public Comparator<? super E> comparator() {
1781 synchronized (mutex) {return ss.comparator();}
1782 }
1783
1784 public SortedSet<E> subSet(E fromElement, E toElement) {
1785 synchronized (mutex) {
1786 return new SynchronizedSortedSet<>(
1787 ss.subSet(fromElement, toElement), mutex);
1788 }
1789 }
1790 public SortedSet<E> headSet(E toElement) {
1791 synchronized (mutex) {
1792 return new SynchronizedSortedSet<>(ss.headSet(toElement), mutex);
1793 }
1794 }
1795 public SortedSet<E> tailSet(E fromElement) {
1796 synchronized (mutex) {
1797 return new SynchronizedSortedSet<>(ss.tailSet(fromElement),mutex);
1798 }
1799 }
1800
1801 public E first() {
1802 synchronized (mutex) {return ss.first();}
1803 }
1804 public E last() {
1805 synchronized (mutex) {return ss.last();}
1806 }
1807 }
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834 public static <T> List<T> synchronizedList(List<T> list) {
1835 return (list instanceof RandomAccess ?
1836 new SynchronizedRandomAccessList<>(list) :
1837 new SynchronizedList<>(list));
1838 }
1839
1840 static <T> List<T> synchronizedList(List<T> list, Object mutex) {
1841 return (list instanceof RandomAccess ?
1842 new SynchronizedRandomAccessList<>(list, mutex) :
1843 new SynchronizedList<>(list, mutex));
1844 }
1845
1846
1847
1848
1849 static class SynchronizedList<E>
1850 extends SynchronizedCollection<E>
1851 implements List<E> {
1852 private static final long serialVersionUID = -7754090372962971524L;
1853
1854 final List<E> list;
1855
1856 SynchronizedList(List<E> list) {
1857 super(list);
1858 this.list = list;
1859 }
1860 SynchronizedList(List<E> list, Object mutex) {
1861 super(list, mutex);
1862 this.list = list;
1863 }
1864
1865 public boolean equals(Object o) {
1866 synchronized (mutex) {return list.equals(o);}
1867 }
1868 public int hashCode() {
1869 synchronized (mutex) {return list.hashCode();}
1870 }
1871
1872 public E get(int index) {
1873 synchronized (mutex) {return list.get(index);}
1874 }
1875 public E set(int index, E element) {
1876 synchronized (mutex) {return list.set(index, element);}
1877 }
1878 public void add(int index, E element) {
1879 synchronized (mutex) {list.add(index, element);}
1880 }
1881 public E remove(int index) {
1882 synchronized (mutex) {return list.remove(index);}
1883 }
1884
1885 public int indexOf(Object o) {
1886 synchronized (mutex) {return list.indexOf(o);}
1887 }
1888 public int lastIndexOf(Object o) {
1889 synchronized (mutex) {return list.lastIndexOf(o);}
1890 }
1891
1892 public boolean addAll(int index, Collection<? extends E> c) {
1893 synchronized (mutex) {return list.addAll(index, c);}
1894 }
1895
1896 public ListIterator<E> listIterator() {
1897 return list.listIterator();
1898 }
1899
1900 public ListIterator<E> listIterator(int index) {
1901 return list.listIterator(index);
1902 }
1903
1904 public List<E> subList(int fromIndex, int toIndex) {
1905 synchronized (mutex) {
1906 return new SynchronizedList<>(list.subList(fromIndex, toIndex),
1907 mutex);
1908 }
1909 }
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923 private Object readResolve() {
1924 return (list instanceof RandomAccess
1925 ? new SynchronizedRandomAccessList<>(list)
1926 : this);
1927 }
1928 }
1929
1930
1931
1932
1933 static class SynchronizedRandomAccessList<E>
1934 extends SynchronizedList<E>
1935 implements RandomAccess {
1936
1937 SynchronizedRandomAccessList(List<E> list) {
1938 super(list);
1939 }
1940
1941 SynchronizedRandomAccessList(List<E> list, Object mutex) {
1942 super(list, mutex);
1943 }
1944
1945 public List<E> subList(int fromIndex, int toIndex) {
1946 synchronized (mutex) {
1947 return new SynchronizedRandomAccessList<>(
1948 list.subList(fromIndex, toIndex), mutex);
1949 }
1950 }
1951
1952 private static final long serialVersionUID = 1530674583602358482L;
1953
1954
1955
1956
1957
1958
1959
1960 private Object writeReplace() {
1961 return new SynchronizedList<>(list);
1962 }
1963 }
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992 public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {
1993 return new SynchronizedMap<>(m);
1994 }
1995
1996
1997
1998
1999 private static class SynchronizedMap<K,V>
2000 implements Map<K,V>, Serializable {
2001 private static final long serialVersionUID = 1978198479659022715L;
2002
2003 private final Map<K,V> m;
2004 final Object mutex;
2005
2006 SynchronizedMap(Map<K,V> m) {
2007 if (m==null)
2008 throw new NullPointerException();
2009 this.m = m;
2010 mutex = this;
2011 }
2012
2013 SynchronizedMap(Map<K,V> m, Object mutex) {
2014 this.m = m;
2015 this.mutex = mutex;
2016 }
2017
2018 public int size() {
2019 synchronized (mutex) {return m.size();}
2020 }
2021 public boolean isEmpty() {
2022 synchronized (mutex) {return m.isEmpty();}
2023 }
2024 public boolean containsKey(Object key) {
2025 synchronized (mutex) {return m.containsKey(key);}
2026 }
2027 public boolean containsValue(Object value) {
2028 synchronized (mutex) {return m.containsValue(value);}
2029 }
2030 public V get(Object key) {
2031 synchronized (mutex) {return m.get(key);}
2032 }
2033
2034 public V put(K key, V value) {
2035 synchronized (mutex) {return m.put(key, value);}
2036 }
2037 public V remove(Object key) {
2038 synchronized (mutex) {return m.remove(key);}
2039 }
2040 public void putAll(Map<? extends K, ? extends V> map) {
2041 synchronized (mutex) {m.putAll(map);}
2042 }
2043 public void clear() {
2044 synchronized (mutex) {m.clear();}
2045 }
2046
2047 private transient Set<K> keySet = null;
2048 private transient Set<Map.Entry<K,V>> entrySet = null;
2049 private transient Collection<V> values = null;
2050
2051 public Set<K> keySet() {
2052 synchronized (mutex) {
2053 if (keySet==null)
2054 keySet = new SynchronizedSet<>(m.keySet(), mutex);
2055 return keySet;
2056 }
2057 }
2058
2059 public Set<Map.Entry<K,V>> entrySet() {
2060 synchronized (mutex) {
2061 if (entrySet==null)
2062 entrySet = new SynchronizedSet<>(m.entrySet(), mutex);
2063 return entrySet;
2064 }
2065 }
2066
2067 public Collection<V> values() {
2068 synchronized (mutex) {
2069 if (values==null)
2070 values = new SynchronizedCollection<>(m.values(), mutex);
2071 return values;
2072 }
2073 }
2074
2075 public boolean equals(Object o) {
2076 synchronized (mutex) {return m.equals(o);}
2077 }
2078 public int hashCode() {
2079 synchronized (mutex) {return m.hashCode();}
2080 }
2081 public String toString() {
2082 synchronized (mutex) {return m.toString();}
2083 }
2084 private void writeObject(ObjectOutputStream s) throws IOException {
2085 synchronized (mutex) {s.defaultWriteObject();}
2086 }
2087 }
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131 public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) {
2132 return new SynchronizedSortedMap<>(m);
2133 }
2134
2135
2136
2137
2138
2139 static class SynchronizedSortedMap<K,V>
2140 extends SynchronizedMap<K,V>
2141 implements SortedMap<K,V>
2142 {
2143 private static final long serialVersionUID = -8798146769416483793L;
2144
2145 private final SortedMap<K,V> sm;
2146
2147 SynchronizedSortedMap(SortedMap<K,V> m) {
2148 super(m);
2149 sm = m;
2150 }
2151 SynchronizedSortedMap(SortedMap<K,V> m, Object mutex) {
2152 super(m, mutex);
2153 sm = m;
2154 }
2155
2156 public Comparator<? super K> comparator() {
2157 synchronized (mutex) {return sm.comparator();}
2158 }
2159
2160 public SortedMap<K,V> subMap(K fromKey, K toKey) {
2161 synchronized (mutex) {
2162 return new SynchronizedSortedMap<>(
2163 sm.subMap(fromKey, toKey), mutex);
2164 }
2165 }
2166 public SortedMap<K,V> headMap(K toKey) {
2167 synchronized (mutex) {
2168 return new SynchronizedSortedMap<>(sm.headMap(toKey), mutex);
2169 }
2170 }
2171 public SortedMap<K,V> tailMap(K fromKey) {
2172 synchronized (mutex) {
2173 return new SynchronizedSortedMap<>(sm.tailMap(fromKey),mutex);
2174 }
2175 }
2176
2177 public K firstKey() {
2178 synchronized (mutex) {return sm.firstKey();}
2179 }
2180 public K lastKey() {
2181 synchronized (mutex) {return sm.lastKey();}
2182 }
2183 }
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247 public static <E> Collection<E> checkedCollection(Collection<E> c,
2248 Class<E> type) {
2249 return new CheckedCollection<>(c, type);
2250 }
2251
2252 @SuppressWarnings("unchecked")
2253 static <T> T[] zeroLengthArray(Class<T> type) {
2254 return (T[]) Array.newInstance(type, 0);
2255 }
2256
2257
2258
2259
2260 static class CheckedCollection<E> implements Collection<E>, Serializable {
2261 private static final long serialVersionUID = 1578914078182001775L;
2262
2263 final Collection<E> c;
2264 final Class<E> type;
2265
2266 void typeCheck(Object o) {
2267 if (o != null && !type.isInstance(o))
2268 throw new ClassCastException(badElementMsg(o));
2269 }
2270
2271 private String badElementMsg(Object o) {
2272 return "Attempt to insert " + o.getClass() +
2273 " element into collection with element type " + type;
2274 }
2275
2276 CheckedCollection(Collection<E> c, Class<E> type) {
2277 if (c==null || type == null)
2278 throw new NullPointerException();
2279 this.c = c;
2280 this.type = type;
2281 }
2282
2283 public int size() { return c.size(); }
2284 public boolean isEmpty() { return c.isEmpty(); }
2285 public boolean contains(Object o) { return c.contains(o); }
2286 public Object[] toArray() { return c.toArray(); }
2287 public <T> T[] toArray(T[] a) { return c.toArray(a); }
2288 public String toString() { return c.toString(); }
2289 public boolean remove(Object o) { return c.remove(o); }
2290 public void clear() { c.clear(); }
2291
2292 public boolean containsAll(Collection<?> coll) {
2293 return c.containsAll(coll);
2294 }
2295 public boolean removeAll(Collection<?> coll) {
2296 return c.removeAll(coll);
2297 }
2298 public boolean retainAll(Collection<?> coll) {
2299 return c.retainAll(coll);
2300 }
2301
2302 public Iterator<E> iterator() {
2303 final Iterator<E> it = c.iterator();
2304 return new Iterator<E>() {
2305 public boolean hasNext() { return it.hasNext(); }
2306 public E next() { return it.next(); }
2307 public void remove() { it.remove(); }};
2308 }
2309
2310 public boolean add(E e) {
2311 typeCheck(e);
2312 return c.add(e);
2313 }
2314
2315 private E[] zeroLengthElementArray = null;
2316
2317 private E[] zeroLengthElementArray() {
2318 return zeroLengthElementArray != null ? zeroLengthElementArray :
2319 (zeroLengthElementArray = zeroLengthArray(type));
2320 }
2321
2322 @SuppressWarnings("unchecked")
2323 Collection<E> checkedCopyOf(Collection<? extends E> coll) {
2324 Object[] a = null;
2325 try {
2326 E[] z = zeroLengthElementArray();
2327 a = coll.toArray(z);
2328
2329 if (a.getClass() != z.getClass())
2330 a = Arrays.copyOf(a, a.length, z.getClass());
2331 } catch (ArrayStoreException ignore) {
2332
2333
2334
2335
2336
2337 a = coll.toArray().clone();
2338 for (Object o : a)
2339 typeCheck(o);
2340 }
2341
2342 return (Collection<E>) Arrays.asList(a);
2343 }
2344
2345 public boolean addAll(Collection<? extends E> coll) {
2346
2347
2348
2349
2350 return c.addAll(checkedCopyOf(coll));
2351 }
2352 }
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380 public static <E> Set<E> checkedSet(Set<E> s, Class<E> type) {
2381 return new CheckedSet<>(s, type);
2382 }
2383
2384
2385
2386
2387 static class CheckedSet<E> extends CheckedCollection<E>
2388 implements Set<E>, Serializable
2389 {
2390 private static final long serialVersionUID = 4694047833775013803L;
2391
2392 CheckedSet(Set<E> s, Class<E> elementType) { super(s, elementType); }
2393
2394 public boolean equals(Object o) { return o == this || c.equals(o); }
2395 public int hashCode() { return c.hashCode(); }
2396 }
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425 public static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s,
2426 Class<E> type) {
2427 return new CheckedSortedSet<>(s, type);
2428 }
2429
2430
2431
2432
2433 static class CheckedSortedSet<E> extends CheckedSet<E>
2434 implements SortedSet<E>, Serializable
2435 {
2436 private static final long serialVersionUID = 1599911165492914959L;
2437 private final SortedSet<E> ss;
2438
2439 CheckedSortedSet(SortedSet<E> s, Class<E> type) {
2440 super(s, type);
2441 ss = s;
2442 }
2443
2444 public Comparator<? super E> comparator() { return ss.comparator(); }
2445 public E first() { return ss.first(); }
2446 public E last() { return ss.last(); }
2447
2448 public SortedSet<E> subSet(E fromElement, E toElement) {
2449 return checkedSortedSet(ss.subSet(fromElement,toElement), type);
2450 }
2451 public SortedSet<E> headSet(E toElement) {
2452 return checkedSortedSet(ss.headSet(toElement), type);
2453 }
2454 public SortedSet<E> tailSet(E fromElement) {
2455 return checkedSortedSet(ss.tailSet(fromElement), type);
2456 }
2457 }
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485 public static <E> List<E> checkedList(List<E> list, Class<E> type) {
2486 return (list instanceof RandomAccess ?
2487 new CheckedRandomAccessList<>(list, type) :
2488 new CheckedList<>(list, type));
2489 }
2490
2491
2492
2493
2494 static class CheckedList<E>
2495 extends CheckedCollection<E>
2496 implements List<E>
2497 {
2498 private static final long serialVersionUID = 65247728283967356L;
2499 final List<E> list;
2500
2501 CheckedList(List<E> list, Class<E> type) {
2502 super(list, type);
2503 this.list = list;
2504 }
2505
2506 public boolean equals(Object o) { return o == this || list.equals(o); }
2507 public int hashCode() { return list.hashCode(); }
2508 public E get(int index) { return list.get(index); }
2509 public E remove(int index) { return list.remove(index); }
2510 public int indexOf(Object o) { return list.indexOf(o); }
2511 public int lastIndexOf(Object o) { return list.lastIndexOf(o); }
2512
2513 public E set(int index, E element) {
2514 typeCheck(element);
2515 return list.set(index, element);
2516 }
2517
2518 public void add(int index, E element) {
2519 typeCheck(element);
2520 list.add(index, element);
2521 }
2522
2523 public boolean addAll(int index, Collection<? extends E> c) {
2524 return list.addAll(index, checkedCopyOf(c));
2525 }
2526 public ListIterator<E> listIterator() { return listIterator(0); }
2527
2528 public ListIterator<E> listIterator(final int index) {
2529 final ListIterator<E> i = list.listIterator(index);
2530
2531 return new ListIterator<E>() {
2532 public boolean hasNext() { return i.hasNext(); }
2533 public E next() { return i.next(); }
2534 public boolean hasPrevious() { return i.hasPrevious(); }
2535 public E previous() { return i.previous(); }
2536 public int nextIndex() { return i.nextIndex(); }
2537 public int previousIndex() { return i.previousIndex(); }
2538 public void remove() { i.remove(); }
2539
2540 public void set(E e) {
2541 typeCheck(e);
2542 i.set(e);
2543 }
2544
2545 public void add(E e) {
2546 typeCheck(e);
2547 i.add(e);
2548 }
2549 };
2550 }
2551
2552 public List<E> subList(int fromIndex, int toIndex) {
2553 return new CheckedList<>(list.subList(fromIndex, toIndex), type);
2554 }
2555 }
2556
2557
2558
2559
2560 static class CheckedRandomAccessList<E> extends CheckedList<E>
2561 implements RandomAccess
2562 {
2563 private static final long serialVersionUID = 1638200125423088369L;
2564
2565 CheckedRandomAccessList(List<E> list, Class<E> type) {
2566 super(list, type);
2567 }
2568
2569 public List<E> subList(int fromIndex, int toIndex) {
2570 return new CheckedRandomAccessList<>(
2571 list.subList(fromIndex, toIndex), type);
2572 }
2573 }
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609 public static <K, V> Map<K, V> checkedMap(Map<K, V> m,
2610 Class<K> keyType,
2611 Class<V> valueType) {
2612 return new CheckedMap<>(m, keyType, valueType);
2613 }
2614
2615
2616
2617
2618
2619 private static class CheckedMap<K,V>
2620 implements Map<K,V>, Serializable
2621 {
2622 private static final long serialVersionUID = 5742860141034234728L;
2623
2624 private final Map<K, V> m;
2625 final Class<K> keyType;
2626 final Class<V> valueType;
2627
2628 private void typeCheck(Object key, Object value) {
2629 if (key != null && !keyType.isInstance(key))
2630 throw new ClassCastException(badKeyMsg(key));
2631
2632 if (value != null && !valueType.isInstance(value))
2633 throw new ClassCastException(badValueMsg(value));
2634 }
2635
2636 private String badKeyMsg(Object key) {
2637 return "Attempt to insert " + key.getClass() +
2638 " key into map with key type " + keyType;
2639 }
2640
2641 private String badValueMsg(Object value) {
2642 return "Attempt to insert " + value.getClass() +
2643 " value into map with value type " + valueType;
2644 }
2645
2646 CheckedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) {
2647 if (m == null || keyType == null || valueType == null)
2648 throw new NullPointerException();
2649 this.m = m;
2650 this.keyType = keyType;
2651 this.valueType = valueType;
2652 }
2653
2654 public int size() { return m.size(); }
2655 public boolean isEmpty() { return m.isEmpty(); }
2656 public boolean containsKey(Object key) { return m.containsKey(key); }
2657 public boolean containsValue(Object v) { return m.containsValue(v); }
2658 public V get(Object key) { return m.get(key); }
2659 public V remove(Object key) { return m.remove(key); }
2660 public void clear() { m.clear(); }
2661 public Set<K> keySet() { return m.keySet(); }
2662 public Collection<V> values() { return m.values(); }
2663 public boolean equals(Object o) { return o == this || m.equals(o); }
2664 public int hashCode() { return m.hashCode(); }
2665 public String toString() { return m.toString(); }
2666
2667 public V put(K key, V value) {
2668 typeCheck(key, value);
2669 return m.put(key, value);
2670 }
2671
2672 @SuppressWarnings("unchecked")
2673 public void putAll(Map<? extends K, ? extends V> t) {
2674
2675
2676
2677
2678
2679 Object[] entries = t.entrySet().toArray();
2680 List<Map.Entry<K,V>> checked = new ArrayList<>(entries.length);
2681 for (Object o : entries) {
2682 Map.Entry<?,?> e = (Map.Entry<?,?>) o;
2683 Object k = e.getKey();
2684 Object v = e.getValue();
2685 typeCheck(k, v);
2686 checked.add(
2687 new AbstractMap.SimpleImmutableEntry<>((K) k, (V) v));
2688 }
2689 for (Map.Entry<K,V> e : checked)
2690 m.put(e.getKey(), e.getValue());
2691 }
2692
2693 private transient Set<Map.Entry<K,V>> entrySet = null;
2694
2695 public Set<Map.Entry<K,V>> entrySet() {
2696 if (entrySet==null)
2697 entrySet = new CheckedEntrySet<>(m.entrySet(), valueType);
2698 return entrySet;
2699 }
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709 static class CheckedEntrySet<K,V> implements Set<Map.Entry<K,V>> {
2710 private final Set<Map.Entry<K,V>> s;
2711 private final Class<V> valueType;
2712
2713 CheckedEntrySet(Set<Map.Entry<K, V>> s, Class<V> valueType) {
2714 this.s = s;
2715 this.valueType = valueType;
2716 }
2717
2718 public int size() { return s.size(); }
2719 public boolean isEmpty() { return s.isEmpty(); }
2720 public String toString() { return s.toString(); }
2721 public int hashCode() { return s.hashCode(); }
2722 public void clear() { s.clear(); }
2723
2724 public boolean add(Map.Entry<K, V> e) {
2725 throw new UnsupportedOperationException();
2726 }
2727 public boolean addAll(Collection<? extends Map.Entry<K, V>> coll) {
2728 throw new UnsupportedOperationException();
2729 }
2730
2731 public Iterator<Map.Entry<K,V>> iterator() {
2732 final Iterator<Map.Entry<K, V>> i = s.iterator();
2733 final Class<V> valueType = this.valueType;
2734
2735 return new Iterator<Map.Entry<K,V>>() {
2736 public boolean hasNext() { return i.hasNext(); }
2737 public void remove() { i.remove(); }
2738
2739 public Map.Entry<K,V> next() {
2740 return checkedEntry(i.next(), valueType);
2741 }
2742 };
2743 }
2744
2745 @SuppressWarnings("unchecked")
2746 public Object[] toArray() {
2747 Object[] source = s.toArray();
2748
2749
2750
2751
2752
2753 Object[] dest = (CheckedEntry.class.isInstance(
2754 source.getClass().getComponentType()) ? source :
2755 new Object[source.length]);
2756
2757 for (int i = 0; i < source.length; i++)
2758 dest[i] = checkedEntry((Map.Entry<K,V>)source[i],
2759 valueType);
2760 return dest;
2761 }
2762
2763 @SuppressWarnings("unchecked")
2764 public <T> T[] toArray(T[] a) {
2765
2766
2767
2768 T[] arr = s.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
2769
2770 for (int i=0; i<arr.length; i++)
2771 arr[i] = (T) checkedEntry((Map.Entry<K,V>)arr[i],
2772 valueType);
2773 if (arr.length > a.length)
2774 return arr;
2775
2776 System.arraycopy(arr, 0, a, 0, arr.length);
2777 if (a.length > arr.length)
2778 a[arr.length] = null;
2779 return a;
2780 }
2781
2782
2783
2784
2785
2786
2787
2788 public boolean contains(Object o) {
2789 if (!(o instanceof Map.Entry))
2790 return false;
2791 Map.Entry<?,?> e = (Map.Entry<?,?>) o;
2792 return s.contains(
2793 (e instanceof CheckedEntry) ? e : checkedEntry(e, valueType));
2794 }
2795
2796
2797
2798
2799
2800
2801 public boolean containsAll(Collection<?> c) {
2802 for (Object o : c)
2803 if (!contains(o))
2804 return false;
2805 return true;
2806 }
2807
2808 public boolean remove(Object o) {
2809 if (!(o instanceof Map.Entry))
2810 return false;
2811 return s.remove(new AbstractMap.SimpleImmutableEntry
2812 <>((Map.Entry<?,?>)o));
2813 }
2814
2815 public boolean removeAll(Collection<?> c) {
2816 return batchRemove(c, false);
2817 }
2818 public boolean retainAll(Collection<?> c) {
2819 return batchRemove(c, true);
2820 }
2821 private boolean batchRemove(Collection<?> c, boolean complement) {
2822 boolean modified = false;
2823 Iterator<Map.Entry<K,V>> it = iterator();
2824 while (it.hasNext()) {
2825 if (c.contains(it.next()) != complement) {
2826 it.remove();
2827 modified = true;
2828 }
2829 }
2830 return modified;
2831 }
2832
2833 public boolean equals(Object o) {
2834 if (o == this)
2835 return true;
2836 if (!(o instanceof Set))
2837 return false;
2838 Set<?> that = (Set<?>) o;
2839 return that.size() == s.size()
2840 && containsAll(that);
2841 }
2842
2843 static <K,V,T> CheckedEntry<K,V,T> checkedEntry(Map.Entry<K,V> e,
2844 Class<T> valueType) {
2845 return new CheckedEntry<>(e, valueType);
2846 }
2847
2848
2849
2850
2851
2852
2853
2854
2855 private static class CheckedEntry<K,V,T> implements Map.Entry<K,V> {
2856 private final Map.Entry<K, V> e;
2857 private final Class<T> valueType;
2858
2859 CheckedEntry(Map.Entry<K, V> e, Class<T> valueType) {
2860 this.e = e;
2861 this.valueType = valueType;
2862 }
2863
2864 public K getKey() { return e.getKey(); }
2865 public V getValue() { return e.getValue(); }
2866 public int hashCode() { return e.hashCode(); }
2867 public String toString() { return e.toString(); }
2868
2869 public V setValue(V value) {
2870 if (value != null && !valueType.isInstance(value))
2871 throw new ClassCastException(badValueMsg(value));
2872 return e.setValue(value);
2873 }
2874
2875 private String badValueMsg(Object value) {
2876 return "Attempt to insert " + value.getClass() +
2877 " value into map with value type " + valueType;
2878 }
2879
2880 public boolean equals(Object o) {
2881 if (o == this)
2882 return true;
2883 if (!(o instanceof Map.Entry))
2884 return false;
2885 return e.equals(new AbstractMap.SimpleImmutableEntry
2886 <>((Map.Entry<?,?>)o));
2887 }
2888 }
2889 }
2890 }
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926 public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K, V> m,
2927 Class<K> keyType,
2928 Class<V> valueType) {
2929 return new CheckedSortedMap<>(m, keyType, valueType);
2930 }
2931
2932
2933
2934
2935 static class CheckedSortedMap<K,V> extends CheckedMap<K,V>
2936 implements SortedMap<K,V>, Serializable
2937 {
2938 private static final long serialVersionUID = 1599671320688067438L;
2939
2940 private final SortedMap<K, V> sm;
2941
2942 CheckedSortedMap(SortedMap<K, V> m,
2943 Class<K> keyType, Class<V> valueType) {
2944 super(m, keyType, valueType);
2945 sm = m;
2946 }
2947
2948 public Comparator<? super K> comparator() { return sm.comparator(); }
2949 public K firstKey() { return sm.firstKey(); }
2950 public K lastKey() { return sm.lastKey(); }
2951
2952 public SortedMap<K,V> subMap(K fromKey, K toKey) {
2953 return checkedSortedMap(sm.subMap(fromKey, toKey),
2954 keyType, valueType);
2955 }
2956 public SortedMap<K,V> headMap(K toKey) {
2957 return checkedSortedMap(sm.headMap(toKey), keyType, valueType);
2958 }
2959 public SortedMap<K,V> tailMap(K fromKey) {
2960 return checkedSortedMap(sm.tailMap(fromKey), keyType, valueType);
2961 }
2962 }
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988 @SuppressWarnings("unchecked")
2989 public static <T> Iterator<T> emptyIterator() {
2990 return (Iterator<T>) EmptyIterator.EMPTY_ITERATOR;
2991 }
2992
2993 private static class EmptyIterator<E> implements Iterator<E> {
2994 static final EmptyIterator<Object> EMPTY_ITERATOR
2995 = new EmptyIterator<>();
2996
2997 public boolean hasNext() { return false; }
2998 public E next() { throw new NoSuchElementException(); }
2999 public void remove() { throw new IllegalStateException(); }
3000 }
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034 @SuppressWarnings("unchecked")
3035 public static <T> ListIterator<T> emptyListIterator() {
3036 return (ListIterator<T>) EmptyListIterator.EMPTY_ITERATOR;
3037 }
3038
3039 private static class EmptyListIterator<E>
3040 extends EmptyIterator<E>
3041 implements ListIterator<E>
3042 {
3043 static final EmptyListIterator<Object> EMPTY_ITERATOR
3044 = new EmptyListIterator<>();
3045
3046 public boolean hasPrevious() { return false; }
3047 public E previous() { throw new NoSuchElementException(); }
3048 public int nextIndex() { return 0; }
3049 public int previousIndex() { return -1; }
3050 public void set(E e) { throw new IllegalStateException(); }
3051 public void add(E e) { throw new UnsupportedOperationException(); }
3052 }
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073 @SuppressWarnings("unchecked")
3074 public static <T> Enumeration<T> emptyEnumeration() {
3075 return (Enumeration<T>) EmptyEnumeration.EMPTY_ENUMERATION;
3076 }
3077
3078 private static class EmptyEnumeration<E> implements Enumeration<E> {
3079 static final EmptyEnumeration<Object> EMPTY_ENUMERATION
3080 = new EmptyEnumeration<>();
3081
3082 public boolean hasMoreElements() { return false; }
3083 public E nextElement() { throw new NoSuchElementException(); }
3084 }
3085
3086
3087
3088
3089
3090
3091 @SuppressWarnings("unchecked")
3092 public static final Set EMPTY_SET = new EmptySet<>();
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110 @SuppressWarnings("unchecked")
3111 public static final <T> Set<T> emptySet() {
3112 return (Set<T>) EMPTY_SET;
3113 }
3114
3115
3116
3117
3118 private static class EmptySet<E>
3119 extends AbstractSet<E>
3120 implements Serializable
3121 {
3122 private static final long serialVersionUID = 1582296315990362920L;
3123
3124 public Iterator<E> iterator() { return emptyIterator(); }
3125
3126 public int size() {return 0;}
3127 public boolean isEmpty() {return true;}
3128
3129 public boolean contains(Object obj) {return false;}
3130 public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
3131
3132 public Object[] toArray() { return new Object[0]; }
3133
3134 public <T> T[] toArray(T[] a) {
3135 if (a.length > 0)
3136 a[0] = null;
3137 return a;
3138 }
3139
3140
3141 private Object readResolve() {
3142 return EMPTY_SET;
3143 }
3144 }
3145
3146
3147
3148
3149
3150
3151 @SuppressWarnings("unchecked")
3152 public static final List EMPTY_LIST = new EmptyList<>();
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169 @SuppressWarnings("unchecked")
3170 public static final <T> List<T> emptyList() {
3171 return (List<T>) EMPTY_LIST;
3172 }
3173
3174
3175
3176
3177 private static class EmptyList<E>
3178 extends AbstractList<E>
3179 implements RandomAccess, Serializable {
3180 private static final long serialVersionUID = 8842843931221139166L;
3181
3182 public Iterator<E> iterator() {
3183 return emptyIterator();
3184 }
3185 public ListIterator<E> listIterator() {
3186 return emptyListIterator();
3187 }
3188
3189 public int size() {return 0;}
3190 public boolean isEmpty() {return true;}
3191
3192 public boolean contains(Object obj) {return false;}
3193 public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
3194
3195 public Object[] toArray() { return new Object[0]; }
3196
3197 public <T> T[] toArray(T[] a) {
3198 if (a.length > 0)
3199 a[0] = null;
3200 return a;
3201 }
3202
3203 public E get(int index) {
3204 throw new IndexOutOfBoundsException("Index: "+index);
3205 }
3206
3207 public boolean equals(Object o) {
3208 return (o instanceof List) && ((List<?>)o).isEmpty();
3209 }
3210
3211 public int hashCode() { return 1; }
3212
3213
3214 private Object readResolve() {
3215 return EMPTY_LIST;
3216 }
3217 }
3218
3219
3220
3221
3222
3223
3224
3225 @SuppressWarnings("unchecked")
3226 public static final Map EMPTY_MAP = new EmptyMap<>();
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243 @SuppressWarnings("unchecked")
3244 public static final <K,V> Map<K,V> emptyMap() {
3245 return (Map<K,V>) EMPTY_MAP;
3246 }
3247
3248
3249
3250
3251 private static class EmptyMap<K,V>
3252 extends AbstractMap<K,V>
3253 implements Serializable
3254 {
3255 private static final long serialVersionUID = 6428348081105594320L;
3256
3257 public int size() {return 0;}
3258 public boolean isEmpty() {return true;}
3259 public boolean containsKey(Object key) {return false;}
3260 public boolean containsValue(Object value) {return false;}
3261 public V get(Object key) {return null;}
3262 public Set<K> keySet() {return emptySet();}
3263 public Collection<V> values() {return emptySet();}
3264 public Set<Map.Entry<K,V>> entrySet() {return emptySet();}
3265
3266 public boolean equals(Object o) {
3267 return (o instanceof Map) && ((Map<?,?>)o).isEmpty();
3268 }
3269
3270 public int hashCode() {return 0;}
3271
3272
3273 private Object readResolve() {
3274 return EMPTY_MAP;
3275 }
3276 }
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287 public static <T> Set<T> singleton(T o) {
3288 return new SingletonSet<>(o);
3289 }
3290
3291 static <E> Iterator<E> singletonIterator(final E e) {
3292 return new Iterator<E>() {
3293 private boolean hasNext = true;
3294 public boolean hasNext() {
3295 return hasNext;
3296 }
3297 public E next() {
3298 if (hasNext) {
3299 hasNext = false;
3300 return e;
3301 }
3302 throw new NoSuchElementException();
3303 }
3304 public void remove() {
3305 throw new UnsupportedOperationException();
3306 }
3307 };
3308 }
3309
3310
3311
3312
3313 private static class SingletonSet<E>
3314 extends AbstractSet<E>
3315 implements Serializable
3316 {
3317 private static final long serialVersionUID = 3193687207550431679L;
3318
3319 private final E element;
3320
3321 SingletonSet(E e) {element = e;}
3322
3323 public Iterator<E> iterator() {
3324 return singletonIterator(element);
3325 }
3326
3327 public int size() {return 1;}
3328
3329 public boolean contains(Object o) {return eq(o, element);}
3330 }
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340 public static <T> List<T> singletonList(T o) {
3341 return new SingletonList<>(o);
3342 }
3343
3344
3345
3346
3347 private static class SingletonList<E>
3348 extends AbstractList<E>
3349 implements RandomAccess, Serializable {
3350
3351 private static final long serialVersionUID = 3093736618740652951L;
3352
3353 private final E element;
3354
3355 SingletonList(E obj) {element = obj;}
3356
3357 public Iterator<E> iterator() {
3358 return singletonIterator(element);
3359 }
3360
3361 public int size() {return 1;}
3362
3363 public boolean contains(Object obj) {return eq(obj, element);}
3364
3365 public E get(int index) {
3366 if (index != 0)
3367 throw new IndexOutOfBoundsException("Index: "+index+", Size: 1");
3368 return element;
3369 }
3370 }
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382 public static <K,V> Map<K,V> singletonMap(K key, V value) {
3383 return new SingletonMap<>(key, value);
3384 }
3385
3386
3387
3388
3389 private static class SingletonMap<K,V>
3390 extends AbstractMap<K,V>
3391 implements Serializable {
3392 private static final long serialVersionUID = -6979724477215052911L;
3393
3394 private final K k;
3395 private final V v;
3396
3397 SingletonMap(K key, V value) {
3398 k = key;
3399 v = value;
3400 }
3401
3402 public int size() {return 1;}
3403
3404 public boolean isEmpty() {return false;}
3405
3406 public boolean containsKey(Object key) {return eq(key, k);}
3407
3408 public boolean containsValue(Object value) {return eq(value, v);}
3409
3410 public V get(Object key) {return (eq(key, k) ? v : null);}
3411
3412 private transient Set<K> keySet = null;
3413 private transient Set<Map.Entry<K,V>> entrySet = null;
3414 private transient Collection<V> values = null;
3415
3416 public Set<K> keySet() {
3417 if (keySet==null)
3418 keySet = singleton(k);
3419 return keySet;
3420 }
3421
3422 public Set<Map.Entry<K,V>> entrySet() {
3423 if (entrySet==null)
3424 entrySet = Collections.<Map.Entry<K,V>>singleton(
3425 new SimpleImmutableEntry<>(k, v));
3426 return entrySet;
3427 }
3428
3429 public Collection<V> values() {
3430 if (values==null)
3431 values = singleton(v);
3432 return values;
3433 }
3434
3435 }
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454 public static <T> List<T> nCopies(int n, T o) {
3455 if (n < 0)
3456 throw new IllegalArgumentException("List length = " + n);
3457 return new CopiesList<>(n, o);
3458 }
3459
3460
3461
3462
3463 private static class CopiesList<E>
3464 extends AbstractList<E>
3465 implements RandomAccess, Serializable
3466 {
3467 private static final long serialVersionUID = 2739099268398711800L;
3468
3469 final int n;
3470 final E element;
3471
3472 CopiesList(int n, E e) {
3473 assert n >= 0;
3474 this.n = n;
3475 element = e;
3476 }
3477
3478 public int size() {
3479 return n;
3480 }
3481
3482 public boolean contains(Object obj) {
3483 return n != 0 && eq(obj, element);
3484 }
3485
3486 public int indexOf(Object o) {
3487 return contains(o) ? 0 : -1;
3488 }
3489
3490 public int lastIndexOf(Object o) {
3491 return contains(o) ? n - 1 : -1;
3492 }
3493
3494 public E get(int index) {
3495 if (index < 0 || index >= n)
3496 throw new IndexOutOfBoundsException("Index: "+index+
3497 ", Size: "+n);
3498 return element;
3499 }
3500
3501 public Object[] toArray() {
3502 final Object[] a = new Object[n];
3503 if (element != null)
3504 Arrays.fill(a, 0, n, element);
3505 return a;
3506 }
3507
3508 public <T> T[] toArray(T[] a) {
3509 final int n = this.n;
3510 if (a.length < n) {
3511 a = (T[])java.lang.reflect.Array
3512 .newInstance(a.getClass().getComponentType(), n);
3513 if (element != null)
3514 Arrays.fill(a, 0, n, element);
3515 } else {
3516 Arrays.fill(a, 0, n, element);
3517 if (a.length > n)
3518 a[n] = null;
3519 }
3520 return a;
3521 }
3522
3523 public List<E> subList(int fromIndex, int toIndex) {
3524 if (fromIndex < 0)
3525 throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
3526 if (toIndex > n)
3527 throw new IndexOutOfBoundsException("toIndex = " + toIndex);
3528 if (fromIndex > toIndex)
3529 throw new IllegalArgumentException("fromIndex(" + fromIndex +
3530 ") > toIndex(" + toIndex + ")");
3531 return new CopiesList<>(toIndex - fromIndex, element);
3532 }
3533 }
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554 public static <T> Comparator<T> reverseOrder() {
3555 return (Comparator<T>) ReverseComparator.REVERSE_ORDER;
3556 }
3557
3558
3559
3560
3561 private static class ReverseComparator
3562 implements Comparator<Comparable<Object>>, Serializable {
3563
3564 private static final long serialVersionUID = 7207038068494060240L;
3565
3566 static final ReverseComparator REVERSE_ORDER
3567 = new ReverseComparator();
3568
3569 public int compare(Comparable<Object> c1, Comparable<Object> c2) {
3570 return c2.compareTo(c1);
3571 }
3572
3573 private Object readResolve() { return reverseOrder(); }
3574 }
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592 public static <T> Comparator<T> reverseOrder(Comparator<T> cmp) {
3593 if (cmp == null)
3594 return reverseOrder();
3595
3596 if (cmp instanceof ReverseComparator2)
3597 return ((ReverseComparator2<T>)cmp).cmp;
3598
3599 return new ReverseComparator2<>(cmp);
3600 }
3601
3602
3603
3604
3605 private static class ReverseComparator2<T> implements Comparator<T>,
3606 Serializable
3607 {
3608 private static final long serialVersionUID = 4374092139857L;
3609
3610
3611
3612
3613
3614
3615
3616
3617 final Comparator<T> cmp;
3618
3619 ReverseComparator2(Comparator<T> cmp) {
3620 assert cmp != null;
3621 this.cmp = cmp;
3622 }
3623
3624 public int compare(T t1, T t2) {
3625 return cmp.compare(t2, t1);
3626 }
3627
3628 public boolean equals(Object o) {
3629 return (o == this) ||
3630 (o instanceof ReverseComparator2 &&
3631 cmp.equals(((ReverseComparator2)o).cmp));
3632 }
3633
3634 public int hashCode() {
3635 return cmp.hashCode() ^ Integer.MIN_VALUE;
3636 }
3637 }
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648 public static <T> Enumeration<T> enumeration(final Collection<T> c) {
3649 return new Enumeration<T>() {
3650 private final Iterator<T> i = c.iterator();
3651
3652 public boolean hasMoreElements() {
3653 return i.hasNext();
3654 }
3655
3656 public T nextElement() {
3657 return i.next();
3658 }
3659 };
3660 }
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677 public static <T> ArrayList<T> list(Enumeration<T> e) {
3678 ArrayList<T> l = new ArrayList<>();
3679 while (e.hasMoreElements())
3680 l.add(e.nextElement());
3681 return l;
3682 }
3683
3684
3685
3686
3687 static boolean eq(Object o1, Object o2) {
3688 return o1==null ? o2==null : o1.equals(o2);
3689 }
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703 public static int frequency(Collection<?> c, Object o) {
3704 int result = 0;
3705 if (o == null) {
3706 for (Object e : c)
3707 if (e == null)
3708 result++;
3709 } else {
3710 for (Object e : c)
3711 if (o.equals(e))
3712 result++;
3713 }
3714 return result;
3715 }
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755 public static boolean disjoint(Collection<?> c1, Collection<?> c2) {
3756
3757
3758 Collection<?> contains = c2;
3759
3760
3761
3762
3763
3764 Collection<?> iterate = c1;
3765
3766
3767
3768
3769
3770
3771 if (c1 instanceof Set) {
3772
3773
3774 iterate = c2;
3775 contains = c1;
3776 } else if (!(c2 instanceof Set)) {
3777
3778
3779
3780
3781
3782
3783 int c1size = c1.size();
3784 int c2size = c2.size();
3785 if (c1size == 0 || c2size == 0) {
3786
3787 return true;
3788 }
3789
3790 if (c1size > c2size) {
3791 iterate = c2;
3792 contains = c1;
3793 }
3794 }
3795
3796 for (Object e : iterate) {
3797 if (contains.contains(e)) {
3798
3799 return false;
3800 }
3801 }
3802
3803
3804 return true;
3805 }
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833 @SafeVarargs
3834 public static <T> boolean addAll(Collection<? super T> c, T... elements) {
3835 boolean result = false;
3836 for (T element : elements)
3837 result |= c.add(element);
3838 return result;
3839 }
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870 public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
3871 return new SetFromMap<>(map);
3872 }
3873
3874
3875
3876
3877 private static class SetFromMap<E> extends AbstractSet<E>
3878 implements Set<E>, Serializable
3879 {
3880 private final Map<E, Boolean> m;
3881 private transient Set<E> s;
3882
3883 SetFromMap(Map<E, Boolean> map) {
3884 if (!map.isEmpty())
3885 throw new IllegalArgumentException("Map is non-empty");
3886 m = map;
3887 s = map.keySet();
3888 }
3889
3890 public void clear() { m.clear(); }
3891 public int size() { return m.size(); }
3892 public boolean isEmpty() { return m.isEmpty(); }
3893 public boolean contains(Object o) { return m.containsKey(o); }
3894 public boolean remove(Object o) { return m.remove(o) != null; }
3895 public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; }
3896 public Iterator<E> iterator() { return s.iterator(); }
3897 public Object[] toArray() { return s.toArray(); }
3898 public <T> T[] toArray(T[] a) { return s.toArray(a); }
3899 public String toString() { return s.toString(); }
3900 public int hashCode() { return s.hashCode(); }
3901 public boolean equals(Object o) { return o == this || s.equals(o); }
3902 public boolean containsAll(Collection<?> c) {return s.containsAll(c);}
3903 public boolean removeAll(Collection<?> c) {return s.removeAll(c);}
3904 public boolean retainAll(Collection<?> c) {return s.retainAll(c);}
3905
3906
3907 private static final long serialVersionUID = 2454657854757543876L;
3908
3909 private void readObject(java.io.ObjectInputStream stream)
3910 throws IOException, ClassNotFoundException
3911 {
3912 stream.defaultReadObject();
3913 s = m.keySet();
3914 }
3915 }
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934 public static <T> Queue<T> asLifoQueue(Deque<T> deque) {
3935 return new AsLIFOQueue<>(deque);
3936 }
3937
3938
3939
3940
3941 static class AsLIFOQueue<E> extends AbstractQueue<E>
3942 implements Queue<E>, Serializable {
3943 private static final long serialVersionUID = 1802017725587941708L;
3944 private final Deque<E> q;
3945 AsLIFOQueue(Deque<E> q) { this.q = q; }
3946 public boolean add(E e) { q.addFirst(e); return true; }
3947 public boolean offer(E e) { return q.offerFirst(e); }
3948 public E poll() { return q.pollFirst(); }
3949 public E remove() { return q.removeFirst(); }
3950 public E peek() { return q.peekFirst(); }
3951 public E element() { return q.getFirst(); }
3952 public void clear() { q.clear(); }
3953 public int size() { return q.size(); }
3954 public boolean isEmpty() { return q.isEmpty(); }
3955 public boolean contains(Object o) { return q.contains(o); }
3956 public boolean remove(Object o) { return q.remove(o); }
3957 public Iterator<E> iterator() { return q.iterator(); }
3958 public Object[] toArray() { return q.toArray(); }
3959 public <T> T[] toArray(T[] a) { return q.toArray(a); }
3960 public String toString() { return q.toString(); }
3961 public boolean containsAll(Collection<?> c) {return q.containsAll(c);}
3962 public boolean removeAll(Collection<?> c) {return q.removeAll(c);}
3963 public boolean retainAll(Collection<?> c) {return q.retainAll(c);}
3964
3965 }
3966 }